﻿using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Controls;
using System.Globalization;

using SBPweb.RIA.Client.Controls.Base;

namespace SBPweb.RIA.Client.Controls.Converters
{

    public class ImageSourceConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType == typeof(ImageSource))
            {
                string path = parameter.ToString();
                if (ExtendableApplication.Current != null && ExtendableApplication.Current.HostBaseUri != null)
                {
                    path = path.Replace("~/", ExtendableApplication.Current.HostBaseUri.OriginalString);
                }
                return new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
                //if (parameter == null)
                //{
                //    parameter = "{0}";
                //}
                //return new BitmapImage(new Uri(string.Format(parameter.ToString(), value.ToString()), UriKind.Relative));
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }

}
